/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.h2t.loveplace.views;

import com.h2t.loveplace.data.LocationTest;
import com.h2t.loveplace.main.LoveplaceMidlet;
import com.h2t.loveplace.models.Place;
import com.h2t.loveplace.utils.PlaceStore;
import com.nokia.maps.kml.ColorStyle;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.CoordinateLayout;
import java.io.IOException;
import javax.microedition.m3g.Background;

/**
 *
 * @author TungHD
 */
public class ViewNewPlace extends Form implements ActionListener {
    
    private LoveplaceMidlet main;
    // private Form fmNewPlace;
    private Label lbTitle;
    private TextField tfTitle;
    private Button btTakePicture;
    private Label lbNote;
    private TextArea taNote;
    private Button btSubmit;
    private Container container;
    private CameraCapture camera;
    private Command cmBack;
    
    public ViewNewPlace(LoveplaceMidlet main) {
        this.main = main;
        initNewPlace();
    }
    
    public void initNewPlace() {
        setTitle("New Place");
        // fmNewPlace = new Form("New Place");
        //  fmNewPlace.
        camera = new CameraCapture(main);
        
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        cmBack = new Command("Back");
        //fmNewPlace.
        setBackCommand(cmBack);
        addCommand(cmBack);
        // fmNewPlace
        addCommandListener(this);
        try {
            // fmNewPlace.
            setBgImage(Image.createImage("/photo/new-bg.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        lbTitle = new Label("Title");
        lbTitle.setX(TOP);
        lbTitle.setY(TOP);
        lbTitle.getStyle().setBgTransparency(1);
        lbTitle.getStyle().setFgColor(1);
        lbTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        
        
        
        tfTitle = new TextField("", 9);
        tfTitle.setX(TOP);
        tfTitle.setY(TOP);
        
        btTakePicture = new Button("Take Picture");
        btTakePicture.setX(TOP);
        btTakePicture.setY(TOP);
        btTakePicture.addActionListener(this);
        
        lbNote = new Label("Note");
        lbNote.setX(TOP);
        lbNote.setY(TOP);
        lbNote.getStyle().setBgTransparency(1);
        lbNote.getStyle().setFgColor(1);
        lbNote.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        
        taNote = new TextArea(5, 7);
        taNote.setX(TOP);
        taNote.setY(TOP);
        
        btSubmit = new Button("Submit");
        btSubmit.setX(TOP);
        btSubmit.setY(TOP);
        btSubmit.addActionListener(this);
//---

        
        container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        //container.setX(40);
        //container.setY(0);
        //container.setSize(new Dimension(220, 374));
        container.addComponent(lbTitle);
        container.addComponent(tfTitle);
        container.addComponent(btTakePicture);
        container.addComponent(lbNote);
        container.addComponent(taNote);
        container.addComponent(btSubmit);

        //  fmNewPlace.
        addComponent(container);
        show();
    }
    
    public void actionPerformed(ActionEvent ae) {
        Object obj = ae.getSource();
        if (obj == cmBack) {
//            camera.close();
            
            main.showMainView();
        } else if (ae.getComponent() == btTakePicture) {
            System.out.println("take pic");
            camera.createCamera();
        }
        else if(ae.getComponent() == btSubmit) {
            String title = tfTitle.getText();
            String detail = taNote.getText();
            if(title.trim().length() <= 0 || detail.trim().length() <= 0) {
                return;
            } else {
                Place place = new Place(title, main.filename, detail, LocationTest.current.getLat(), LocationTest.current.getLon());
                System.out.println("File: " + main.filename);
                PlaceStore.addToAlbum(place);
                System.out.println("OKKKK");
                main.showMainView();
            }
        }
    }
}
